<template>
  <div class="panel">
    <el-dialog :title="form.id ? '修改权限' : '新增权限'" :visible.sync="showDialog" @close="cancel">
      <el-form :model="form" label-width="100px">
        <el-form-item label="权限名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="权限标识">
          <el-input v-model="form.code"></el-input>
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="form.description"></el-input>
        </el-form-item>
        <el-form-item label="企业可见" class="switch">
          <!-- <el-input v-model="form.enVisible"></el-input> -->
          <el-switch
            style="display: block"
            v-model="form.enVisible"
            active-value="1"
            inactive-value="0"
            active-text="可见"
            inactive-text="不可见"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getDetailPermissionApi,
  updatePermissionApi,
  addPermissionApi,
} from "@/api/permission.js";
export default {
  // showDialog:控制面板的显示与隐藏
  props: ["showDialog"],
  data() {
    return {
      // 数据源
      form: {
        name: "",
        code: "",
        description: "",
        enVisible: "",
      },
    };
  },
  methods: {
    //     两个新增按钮：一个用来添加页面权限 & 一个用来添加按钮权限
    // 页面权限： type: 1, pid: "0"
    // 按钮权限： type: 2, pid：点谁是谁的 id
    // 得到type&pid的值
    getData(type, pid) {
      this.form.type = type;
      this.form.pid = pid;
    },
    // 根据ID获取权限点详情
    async getDetailPermission(id) {
      const res = await getDetailPermissionApi(id);
      //  console.log(res);
      this.form = res.data;
    },

    cancel() {
      //  关闭面板
      this.$emit("update:showDialog", false);
      //   清空数据
      this.form = {
        name: "",
        code: "",
        description: "",
        enVisible: "",
      };
    },
    // 确定按钮
    async submit() {
      if (this.form.id) {
        // 修改数据
        const res = await updatePermissionApi(this.form);
        // console.log(res);
      } else {
        // 新增数据
        const res = await addPermissionApi(this.form);
        // console.log(res);
      }

      // 提示操作成功
    this.$message.success(this.form.id ? '修改成功' : '新增成功')

      //  关闭面板
      this.$emit("update:showDialog", false);

      // 更新数据
      this.$emit("updateData");
      //   清空数据
      this.form = {
        name: "",
        code: "",
        description: "",
        enVisible: "",
      };
    },
  },
  created() {
  },
};
</script>

<style lang="scss">
.switch {
  position: relative;
  .el-switch {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
  }
}
</style>